package com.help10000.rms.ui.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.amir.common.beans.FeedBackItem
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BaseRecyclerViewAdapter
import com.amir.common.utils.DisplayUtil
import com.help10000.rms.R
import com.help10000.rms.common.GridSpacingItemDecoration
import com.help10000.rms.mvp.presenter.FeedbackListPresenter
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.item_feedback_layout.view.*

class FeedbackListAdapter(
    context: Context?,
    dataList: List<FeedBackItem>,
    presenter: FeedbackListPresenter
) :
    BaseRecyclerViewAdapter<FeedBackItem>(context, dataList) {

    private var mPresenter = presenter
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        var view = layoutInflater.inflate(R.layout.item_feedback_layout, parent, false)
        return CommonViewHolder(view)
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        var item = dataList[position]
        holder.itemView.apply {
            CreateDate.text = item.CreateDate
            Comments.text = item.Comments
            IsReply.text =
                if (item.IsReply) "已回复" else "待回复"
            if (item.IsReply) {
                IsReply.setTextColor(mContext.resources.getColor(R.color.green))
                Reply.visibility = View.VISIBLE
                ReplyDate.visibility = View.VISIBLE
                labelReplyDate.visibility = View.VISIBLE
                Reply.text = item.Reply
                ReplyDate.text = item.ReplyDate
            } else {
                IsReply.setTextColor(mContext.resources.getColor(R.color.red2))
                Reply.visibility = View.GONE
                ReplyDate.visibility = View.GONE
                labelReplyDate.visibility = View.GONE
            }
            var photoItemAdapter =
                OrderPhotoItemAdapter(mContext, item.CallPhotos, R.layout.item_order_photo_item)
            recyclerViewCell?.apply {
                layoutManager = GridLayoutManager(context, 4)
                adapter = photoItemAdapter
            }
            var padding = DisplayUtil.dip2px(context, 5f)
            recyclerViewCell?.addItemDecoration(GridSpacingItemDecoration(padding, true))
        }
        holder.setOnClickListener(R.id.btnDelete) {
            XPopup.Builder(mContext)
                .asConfirm(
                    null, "确定删除该条反馈吗？"
                ) {
                    mPresenter.deleteFeed(item.FeelbackID, position)
                }.show()
        }

    }

    override fun getItemCount(): Int {
        return dataList.size
    }

}
